// miniprogram/pages/Admin/command/command.js
var app = getApp()
var utils = require('../../../utils/utils')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: [],
    array_name:[],
    index:0,
    index2:0,
    device_num:null,
    array_command:["前去修理","检查设备情况","关闭设备",""]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      device_num:app.globalData.command_device_num
    })
    var that = this
    const db = wx.cloud.database()
    db.collection('personnel').where({
      identity:"employee"
    })
    .get({
      success: function(res){
        let name = res.data.map(obj => {return obj.name}) 
        // console.log(name) 
        that.setData({
          array_name:name,
          array:res.data
        })
      }
    })
  },
  bindPickerChange: function(e) {
    this.setData({
      index: e.detail.value
    })
  },
  bindPickerChange2: function(e) {
    this.setData({
      index2: e.detail.value
    })
  },
  submit: function(e){
    var engineer = this.data.array_name[e.detail.value.pick1]
    var instruction = this.data.array_command[e.detail.value.pick2]
    var time = utils.formatTime(new Date())
    const db = wx.cloud.database()
    db.collection('command').add({
      data:{
        device_num:this.data.device_num,
        engineer:engineer,
        instruction:instruction,
        remarks:e.detail.value.remarks,
        done:false,
        time:time
      },
      success: function(res){
        // console.log(res)
        wx.navigateTo({
          url: '../logs/logs',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})